import { ref, reactive } from "vue";
import { defineStore } from "pinia";
import { secure } from "@/utils/secure-ls";

//定义持久化store
//npm i pinia-plugin-persistedstate
const authStore = defineStore(
    "auth",
    () => {
        //access_token
        const accessToken = ref("");
        const setAccessToken = (token: string): void => {
            accessToken.value = token;
        };

        //refresh_token
        const refreshToken = ref("");
        const setRefreshToken = (token: string): void => {
            refreshToken.value = token;
        };

        //routes路由
        const routes = ref<Array<any>>([]);
        const setRoutes = (routers: Array<any>): void => {
            routes.value = routers;
        };

        //左侧菜单折叠状态
        const menuCollapse = ref(false);
        const setMenuCollapse = (collapse: boolean): void => {
            menuCollapse.value = collapse;
        };

        //面包屑列表
        const breadcrumbList = ref<Array<any>>([]);
        const setBreadcrumbList = (list: Array<any>): void => {
            breadcrumbList.value = list;
        };

        //路由资源信息列表
        const routerResourceList = ref<Record<string, string[]>>({});
        const setRouterResourceList = (value: Record<string, string[]>): void => {
            routerResourceList.value = value;
        };

        //Home页背景透明度
        const homeBackgroundOpacity = ref<number>(10);
        const setHomeBackgroundOpacity = (opacity: number): void => {
            homeBackgroundOpacity.value = opacity;
        };

        //考试的答案
        const examAnswerMap = reactive(new Map<number, string[]>());
        const setExamAnswerMap = (key: number, value: string[]): void => {
            examAnswerMap.set(key, value);
        };
        const clearExamAnswerMap = () => {
            examAnswerMap.clear();
        };

        //clear情况
        const clear = () => {
            setAccessToken("");
            setRefreshToken("");
            setRoutes([]);
            setMenuCollapse(false);
            setBreadcrumbList([]);
            setRouterResourceList({});
            clearExamAnswerMap()
        };

        return {
            accessToken,
            setAccessToken,
            refreshToken,
            setRefreshToken,
            routes,
            setRoutes,
            menuCollapse,
            setMenuCollapse,
            breadcrumbList,
            setBreadcrumbList,
            routerResourceList,
            setRouterResourceList,
            homeBackgroundOpacity,
            setHomeBackgroundOpacity,
            examAnswerMap,
            setExamAnswerMap,
            clearExamAnswerMap,
            clear,
        };
    },
    {
        persist: {
            key: "lemon-exam-web",
            storage: {
                getItem: (key: string) => secure.get(key),
                setItem: (key: string, value: any) => secure.set(key, value),
            },
        },
    }
);

//定义store，非持久化
const userStore = defineStore(
    "user",
    () => {
        return {};
    },
    { persist: false }
);

//暴露
export { authStore, userStore };
